#!/usr/bin/env python

import os, sys, mysql
 
print len(sys.argv)

if len(sys.argv) < 8 or  len(sys.argv) > 9:
	print 'cl_charger_taut.py  <directory with charger.py and tautomerizer.py> <supplier> <Supplier Id numer field> <username> <password> <project> <initial> <comment (optional)>'
	sys.exit(1)

path =  sys.argv[1]
supplier = sys.argv[2]
id_field = sys.argv[3]
username = sys.argv[4]
password = sys.argv[5]
project = sys.argv[6]
initial = sys.argv[7]

if len(sys.argv) == 9:
	comment = sys.argv[8]
else:
	comment = ''


#check db connection
conn=mysql.connect2server(password, username,'purchasable')  

cursor = conn.cursor ()

#check project
command = 'select unique_compounds, supplier_info_table, supplier_table from projects where project = "' + project + '"'
print command
cursor.execute(command)
results=cursor.fetchall()
if len(results) == 0:
	print 'project does not exist'
	sys.exit(1)

table = results[0][0]
sup_info_table = results[0][1]
sup_table = results[0][2]

#get supplier_fied from supplier_table
command = "show fields from " + sup_table
cursor.execute(command)
results=cursor.fetchall()
for result in results:
	if result[3] == 'PRI':
		supplier_field = result[0]
		break

#check supplier
command = "SELECT prefix from " + sup_table + " where " + supplier_field + " = '" + supplier + "'"
cursor.execute(command)
results = cursor.fetchall()
if len (results) <> 1:
	print 'something wrong with your supplier or centre'
	sys.exit(1)

conn.close()


files = os.listdir('./')

clean_list = []
max_file_number = 0

#remove all none .smi from files
for i in files:
	if (i[-4:] == '.sdf') or (i[-4:] == '.SDF'):
		clean_list.append(i)

files = clean_list

path = sys.argv[1]


files.sort()


#make start file, suitable for array job, assumes that smiles files are numbered
#assume that counting starts with 1	for i in files:
#first_file = files[0]
#prefix = first_file[0:first_file.find('_')]
#print prefix
#start_file = open(prefix + '.bin', 'w')

#start_file.write('#BSUB -q short\n')
#start_file.write('#BSUB -J ' + prefix + '_charg_taut_Array[1-' + str(max_file_number) + ']\n')
#start_file.write('#BSUB -o "%J.output.%I"\n')
#start_file.write('#BSUB -e "%J.err.%I"\n')


#start_file.write(path + 'charger.py ' + prefix + '_\$LSB_JOBINDEX' + first_file[-4:] + ' ' + prefix + '_charged_\$LSB_JOBINDEX' + first_file[-4:] + ' False 7 0' + ' ' + id_field)

#start_file.write(path + '/tautomerizer.py ' + prefix + '_\$LSB_JOBINDEX.smi ' + prefix + '_taut_\$LSB_JOBINDEX.smi')
#start_file.close()

#os.system('chmod 755 ' + prefix + '.bin') 
#command = 'bsub ' + prefix + '.bin'

#print command
#os.system(command)

for i in files:
	file_name = i[:-4] + '_start.bin'
	start_file = open(file_name, 'w')
	start_file.write("#BSUB-q short             	# Job queue\n#BSUB-o job.output              # output is sent to file job.output\n#BSUB-J test_mogon              # name of the job\n")
	start_file.write(path + '/charger.py  ')
	start_file.write(i + ' ' + i[:-4] + '_charged.smi False 7 0 '  + id_field + ' > ' + i[:-4] + '_charger.log\n')

	start_file.write(path + '/tautomerizer.py  ')
	start_file.write(i[:-4] + '_charged.smi' + ' ' + i[:-4] + '_charged_taut.smi False  > ' + i[:-4] + '_taut.log\n')

	start_file.write(path + '/mysql_load_unique_smiles.py  ')
	start_file.write(username + ' ' +  password + ' ' + i[:-4] + '_charged_taut.smi' + ' ' + project  + ' ' + initial + ' ' + supplier + ' ' + comment)


	start_file.close()
	os.system('chmod 744 '  + file_name)
	command = 'qsub -q long ' + file_name
	print command
	os.system(command)



	
	
